'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2012 Jens Frenkel
'''
import unittest
import os
from wrapper.jModelica import JModelica

class Test(unittest.TestCase):


    def test_translate(self):
  
        # new directory
        workdir = './testJModelicatranslate'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testJModelica.mo', 'w' )
        f.write('model testJModelica\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testJModelica;\n')
        f.close()   
        
        jmo = JModelica()
        
        res, dt = jmo.translate('testJModelica',['testJModelica.mo'])
        
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        os.rmdir(workdir)   

    def test_simulate(self):
  
        # new directory
        workdir = './testJModelicasimulate'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testJModelica.mo', 'w' )
        f.write('model testJModelica\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testJModelica;\n')
        f.close()   
                
        jmo = JModelica()
        
        res, dt = jmo.simulate('testJModelica',['testJModelica.mo'],0,0.1,1)
        
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        #os.rmdir(workdir)   

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_flatten']
    unittest.main()